<?php
/*
Plugin Name: Share Center
Plugin URI: http://byoutique.com/
Description: Add a convenient Social Sharing Toolbar after the footer of our theme with the Share Center. This will add Facebook Like, Twitter Tweet, Google Plus, LinkedIn Share and StumbleUpon Stumble buttons and boxes to your content so readers can easily share your content.
Version: 1.5
Author: Michele de Chiara
Author URI: http://michele.dechiara.org
License: GPLv2
Note: this plugin derives from the initial start of MikesPickz Web Solutions, Inc. but is completely redesigned according to different requirements
*/

/*
    Copyright 2011  byoutique.com, Inc.  (email : michele@dechiara.org)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

//Initiate Options
function share_center_init() {
    register_setting('sc_options', 'share_center_options', 'share_center_options_validate');
}
add_action('admin_init', 'share_center_init');

// Add translation capabilities
load_plugin_textdomain( 'sc', 'wp-content/plugins/share-center/languages/' );

//Create Settings Page
function share_center_add_page() {
    add_options_page('Share Center Options', 'Share Center', 'manage_options',  __FILE__, 'share_center_options_page');
}
add_action('admin_menu', 'share_center_add_page');

//Actual Content of Settings Page
function share_center_options_page() {
    if (!current_user_can('manage_options'))  {
        wp_die( __('You do not have sufficient permissions to access this page.') );
    } ?>
    <div class="wrap">
    <div class="icon32" id="icon-tools"><br /></div><h2><? echo __('Settings:', 'sc'); ?></h2>
    <form method="post" action="options.php">
    <?php settings_fields('sc_options'); ?>
    <?php $options = get_option('share_center_options'); ?>
    <? //print_r($options); ?>
        <table class="form-table">
        <tr valign="top">
        <th scope="row"><strong><? echo __('Enable Share center:', 'sc')?></strong></th>
        <td><input type="checkbox" name="share_center_options[enable_share_center]" value="1" <?php checked('1', $options['enable_share_center']); ?> /> <? echo __('tips enable share center', 'sc') ?></td>
        </tr>
        <tr valign="top">
        <th scope="row"><strong><? echo __('Twitter User Name', 'sc')?></strong></th>
        <td>@<input style="width: 400px;" type="text" name="share_center_options[share_center_twitter_name]" value="<?php echo stripslashes ($options['share_center_twitter_name']); ?>" /> <small><? echo __('Name to display at the end of Tweets posted with Tweet button', 'sc')?></small></td>
        </tr>
        </table>
        <p class="submit">
        <input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
        </p>
    </form>
</div>
<?php
}

//Validate the options before database insertion
function share_center_options_validate($input) {
    if ( ! isset( $input['enable_share_center'] ) )
        $input['enable_share_center'] = null;
        $input['enable_share_center'] = ( $input['enable_share_center'] == 1 ? 1 : 0 );

    $input['share_center_twitter_name'] =  wp_filter_post_kses($input['share_center_twitter_name']);

    return $input;
}

//Add Settings Link on Plugins Page
function share_center_add_settings_link($links, $file) {
    static $this_plugin;
    if (!$this_plugin) $this_plugin = plugin_basename(__FILE__);

    if ($file == $this_plugin){
        $settings_link = '<a href="admin.php?page=share-center/share-center.php">'.__("Settings", "sc").'</a>';
        array_unshift($links, $settings_link);
    }
    return $links;
}
add_filter('plugin_action_links', 'share_center_add_settings_link', 10, 2);

//<head> Includes
function share_center_head_includes() {
    //The animation with jquery to load
    // Per ora recupera jquery dalla theme di byoutique-theme # (e se lo volessi sharare? )
    // <script type=\"text/javascript\" src=\"http://byoutique.com/wp-content/themes/byoutique-theme/js/jquery.js\"></script>
    $js = "
        <script type=\"text/javascript\">
    //<![CDATA[
            jQuery(init_share_bar);
            function init_share_bar() {
              jQuery('#button-moveRight').click( function() {
                jQuery('#bottom-share-center').animate( {
                  right: '-330px',
                  opacity: 1,
                } );  } );

              jQuery('#button-moveLeft').click( function() {
                jQuery('#bottom-share-center').animate( {
                  right: 0,
                  opacity: 1,
                } );
              } );

            }
    //]]>
        </script>
    ";
    //The styles to load
    $css = "
    <!-- Share Center Styles-->
    <style>
    /* Share center (specific for byoutique.com )------------ */
    #bottom-share-center{ height: 64px; position: fixed; z-index: 999; right: -330px; bottom: 20px; border: 1px solid #ccc; border-right: 0px; background: #9DC2CE; padding: 5px; padding-right: 0px; font-family: Arial; color: #333; border-top-left-radius: 10px; border-bottom-left-radius: 10px;}
    #bottom-share-center ul{ padding-left: 0px; margin: 0;}
    #bottom-share-center ul li{ margin-right: 5px; float: left; list-style:none; display: inline-block; margin-left: 0px; }
    #bottom-share-center #button-moveRight{ cursor: pointer; margin-left: -2px; height: 64px; width: 20px; border: 1px solid #ccc; background: #5B7F8B; }
    #bottom-share-center #button-moveLeft{ cursor: pointer; margin-left: 5px; height: 64px; width: 20px; border: 1px solid #ccc; background: #5B7F8B; }
    #bottom-share-center a{ text-decoration: none; color: #333; }
    /* Fine Share center ------------------------------------ */
    </style>\n";

    echo $js.$css;
}
add_action('wp_head', 'share_center_head_includes');

//Takes the first image in a post and sets it as the thumbnail for the Facebook Like Button
function share_center_fb_like_thumbnails() {
    global $posts;
    $default = 'http://byoutique.com/wp-content/themes/byoutique-theme/img/logo_you.jpg';
    $content = $posts[0]->post_content; // $posts is an array, fetch the first element
    $output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
    if ( $output > 0 )
        $thumb = $matches[1][0];
    else
        $thumb = $default;

    echo "\n<link rel=\"image_src\" href=\"$thumb\" /><!-- Facebook Like Thumbnail -->\n";
}
add_action('wp_head', 'share_center_fb_like_thumbnails' );

//Footer Includes
function share_center_footer_includes() {
    //The scripts to load
    $a = '<!-- Share Center Scripts -->';
    $twitter = '<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script><!--Twitter Tweet Button-->';
    $google = '<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script><!--Google +1 Button-->';
    $linkedin = '<script type="text/javascript" src="http://platform.linkedin.com/in.js"></script><!--LinkedIn Share Button-->';
    echo $a.$twitter.$google.$linkedin;
}
add_action('wp_footer', 'share_center_footer_includes');

//Add the Share Center after the footer
function share_center_bar() {
    $long_link = urlencode(get_permalink($post->ID));
    $short_link = urlencode(wp_get_shortlink(get_the_ID()));
    //Boxes for below the post
    $share_center_bar  = "<div id='bottom-share-center'>";
    $share_center_bar .= "<ul>";
    $share_center_bar .= "<li><button id='button-moveLeft'>&laquo;</button></li>";
    $share_center_bar .= "<li><script type='in/share' data-counter='top'></script></li>";
    $share_center_bar .= "<li><a href='http://twitter.com/share?url=$short_link&amp;counturl=$long_link' class='twitter-share-button' data-count='vertical' data-via='$twitter @byoutique'>Tweet</a></li>";
    $share_center_bar .= "<li><g:plusone size='tall'></g:plusone></li>";
    $share_center_bar .= "<li><script src='http://www.stumbleupon.com/hostedbadge.php?s=5'></script></li>";
    $share_center_bar .= '<li><iframe src="http://www.facebook.com/plugins/like.php?href='.$long_link.'&amp;send=false&amp;layout=box_count&amp;width=67&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:67px; height:62px;" allowTransparency="true"></iframe></li>';
    $share_center_bar .= "<li><button id='button-moveRight'>&raquo;</button></li>";
    $share_center_bar .= "</ul>";
    $share_center_bar .= "<div class='clear'></div>";
    $share_center_bar .= "</div>";

    echo $share_center_bar;
}
add_filter('wp_footer', 'share_center_bar');

//Delete Database fields on deactivation
/*function mp_share_center_remove() {
    delete_option('mp_share_center_options');
}
register_deactivation_hook( __FILE__, 'mp_share_center_remove');*/
?>
